<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单编号" prop="productCode">
      <el-input v-model="dataForm.productCode" placeholder="订单编号"></el-input>
    </el-form-item>
    <el-form-item label="重量" prop="weight">
      <el-input v-model="dataForm.weight" placeholder="重量"></el-input>
    </el-form-item>
    <el-form-item label="价格" prop="price">
      <el-input v-model="dataForm.price" placeholder="价格"></el-input>
    </el-form-item>
    <el-form-item label="副标题" prop="subTitle">
      <el-input v-model="dataForm.subTitle" placeholder="副标题"></el-input>
    </el-form-item>
    <el-form-item label="商品标题" prop="productTitle">
      <el-input v-model="dataForm.productTitle" placeholder="商品标题"></el-input>
    </el-form-item>
    <el-form-item label="商品描述" prop="productDesc">
      <el-input v-model="dataForm.productDesc" placeholder="商品描述"></el-input>
    </el-form-item>
    <el-form-item label="库存" prop="num">
      <el-input v-model="dataForm.num" placeholder="库存"></el-input>
    </el-form-item>
    <el-form-item label="运费" prop="sendPrice">
      <el-input v-model="dataForm.sendPrice" placeholder="运费"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          productCode: '',
          defaultPic: '',
          weight: '',
          price: '',
          subTitle: '',
          productTitle: '',
          productDesc: '',
          num: '',
          sendPrice: '',
        },
        dataRule: {
          productCode: [
            { required: true, message: '订单编号不能为空', trigger: 'blur' }
          ],
          // defaultPic: [
          //   { required: true, message: '默认图片不能为空', trigger: 'blur' }
          // ],
          // wareId: [
          //   { required: true, message: '所属仓库id不能为空', trigger: 'blur' }
          // ],
          // cateId: [
          //   { required: true, message: '所属分类id不能为空', trigger: 'blur' }
          // ],
          weight: [
            { required: true, message: '重量不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '价格不能为空', trigger: 'blur' }
          ],
          subTitle: [
            { required: true, message: '副标题不能为空', trigger: 'blur' }
          ],
          productTitle: [
            { required: true, message: '商品标题不能为空', trigger: 'blur' }
          ],
          productDesc: [
            { required: false, message: '商品描述不能为空', trigger: 'blur' }
          ],
          num: [
            { required: true, message: '库存不能为空', trigger: 'blur' }
          ],
          // showStatus: [
          //   { required: true, message: '是否显示不能为空', trigger: 'blur' }
          // ],
          sendPrice: [
            { required: true, message: '运费不能为空', trigger: 'blur' }
          ],
          // deleted: [
          //   { required: true, message: '是否删除不能为空', trigger: 'blur' }
          // ],
          // version: [
          //   { required: true, message: '不能为空', trigger: 'blur' }
          // ],
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/app/product/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.productCode = data.product.productCode
                this.dataForm.defaultPic = data.product.defaultPic
                // this.dataForm.wareId = data.product.wareId
                // this.dataForm.cateId = data.product.cateId
                this.dataForm.weight = data.product.weight
                this.dataForm.price = data.product.price
                this.dataForm.subTitle = data.product.subTitle
                this.dataForm.productTitle = data.product.productTitle
                this.dataForm.productDesc = data.product.productDesc
                this.dataForm.num = data.product.num
                this.dataForm.sendPrice = data.product.sendPrice
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/app/product/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'productCode': this.dataForm.productCode,
                'defaultPic': this.dataForm.defaultPic,
                // 'wareId': this.dataForm.wareId,
                // 'cateId': this.dataForm.cateId,
                'weight': this.dataForm.weight,
                'price': this.dataForm.price,
                'subTitle': this.dataForm.subTitle,
                'productTitle': this.dataForm.productTitle,
                'productDesc': this.dataForm.productDesc,
                'num': this.dataForm.num,
                // 'showStatus': this.dataForm.showStatus,
                'sendPrice': this.dataForm.sendPrice,
                // 'deleted': this.dataForm.deleted,
                // 'version': this.dataForm.version,
                // 'createTime': this.dataForm.createTime,
                // 'modifyTime': this.dataForm.modifyTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
